//This test interact with excel data file.  
//Reads the contents of cells and put into google search
//Takes screenshots for each search

package Advanced;

import java.io.File;
import java.util.Vector;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class AdvancedSearch  {
	
	public static Vector  paramData=new Vector();
	
	WebDriver driver = new ChromeDriver();
	//Have a new instance for test data; td = the file's handle
	TestData td = new TestData("C:\\Users\\Alice\\Desktop\\eclipse\\samples\\XLTestData.xls");
	@Before
	public void setUp() throws Exception {
	//Read data from the excel file
	td.getDatafromXL();
	paramData=td.getParamData();
	//Get URL
	driver.get((String) paramData.get(0));
	}

	@Test
	public void testAdvancedSearch() throws Exception {
		//How many parameters
		int NumberOfParam = td.getParamCount();

		for(int i = 1;i<NumberOfParam;i++){
			
		driver.findElement(By.xpath("//*[@id='gbqfq']")).clear();
		driver.findElement(By.xpath("//*[@id='gbqfq']")).sendKeys((String) paramData.get(i));
		driver.findElement(By.xpath("//*[@id='gbqfb']")).click();
		
		synchronized (driver)
		{
		    driver.wait(3000);
		}
		
		File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		// Now you can do whatever you need to do with it, for example copy somewhere
		FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot_" + (String)paramData.get(i) + ".png"));
		}
	
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		
	}
}
